//引入组件库
var do_Page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var hashdata = mm("do_HashData");
var communityUtil = require("helper/community");
var openHelper = require("helper/open");
var root = ui("$");
var userUtil = require("helper/user");
var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);
var noData = [ {
	"template" : 1
} ];
var do_ALayout_filter = ui("do_ALayout_filter");
var interests = "";
var paramIn= {"sexKey":"","interestKey":"","industryKey":"","delicacyKey":"","hometownKey":"","entertainmentKey":""};
// UI
var aLayoutRoot = ui("aLayoutRoot");
var listView = ui("listView");
// 定义变量
var page = 1;
var type;
var userId = userUtil.userId();
var communityId = communityUtil.communityVisitId();
var lastDate = userUtil.loadUserPara(userId,'lastQueryDate-'+communityId);
userUtil.saveUserPara(userId,'lastAccessDate-'+communityId,parseInt(new Date().getTime()/1000).toString());
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
//邻居筛选
var selectInterestsUi = root.add("selectIntests","source://view/community/neighbor/filter/filter.ui",0,0);
// 左上角-返回
var headerLeft = ui("do_ALayout_headerLeft");
headerLeft.on("touch", function() {
	do_App.closePage()
});
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	isFriend=true;
	isOther=true;
	binddata();
});
do_ALayout_filter.on("touch", "", 300, function() {
	do_Page.hideKeyboard();
	do_Page.fire("interests");
	do_Page.fire("getInterests", paramIn);
});
// 选择爱好后返回刷新
do_Page.on("refreshInterest", function(data) {
	paramIn.sexKey=data.sexKey;
	paramIn.interestKey=data.interestKey;
	paramIn.industryKey=data.industryKey;
	paramIn.delicacyKey=data.delicacyKey;
	paramIn.hometownKey=data.hometownKey;
	paramIn.entertainmentKey=data.entertainmentKey;
	
	//ui("do_Label_1").text = data.sexKey+''+ data.interestKey;
	/*if (null != data && '' != data.interests) {
		ui("do_Label_1").text = data.interests;
	} else {
		ui("do_Label_1").text = "筛选";
	}
	interests = data.interests;*/
	isFriend=true;
	isOther=true;
	isLast = true;
	isLastLast = true;
	binddata();
});

// 当前页码
var page = 1;
// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.base.neighborPage;
var isFriend=true,isOther=true,isLast=true,isLastLast=true;
// 全部刷新数据
function processData(data){
	if (data.page) {
		if (data.page.content.length) {
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].showTop=false;
				if(data.page.content[i].isNew==0) //不是新增邻居
			    {
				 if(data.page.content[i].isFriend==1&&isFriend){
					isFriend=false;
					data.page.content[i].showTop=true;
					data.page.content[i].topText="好友邻居";
					data.page.content[i].topCountText="（"+data.friendCount+"）";
					
				 }else if(data.page.content[i].isFriend==2&&isOther){
					isOther=false;
					data.page.content[i].showTop=true;
					data.page.content[i].topText="其他邻居";
					data.page.content[i].topCountText="（"+data.otherCount+"）";
					
				 }
			    }
				 else if(data.page.content[i].isNew==1&&isLast) //本次新增邻居
					 {
					   isLast=false;
					   data.page.content[i].showTop=true;
					   data.page.content[i].topText="新增邻居";
					   data.page.content[i].topCountText="（"+data.isNewCount+"）";
					 }				 
				
			}
			listdata.addData(data.page.content);
		} else {
			listdata.addData(noData);
		}
	}
	
}
function binddata() {
	
	page = 1;
	var params = {
		"lastClick": lastDate,
		"currentClick": parseInt(new Date().getTime()/1000).toString(),
		page : page,
		communityId : communityUtil.communityVisitId(),
		interests : paramIn.interestKey,
		sex : paramIn.sexKey,
		profession:paramIn.industryKey,
		delicacys:paramIn.delicacyKey,
		hometown:paramIn.hometownKey,
		entertainment:paramIn.entertainmentKey
		
	}
	qqHttp.post(httpUrl, params, function(data) {
		listview.rebound();
		listdata.removeAll();// 清除数据
		if(!data.isJoin){
			noData = [ {
				"template" : 1,labelContent:"未加入该社区"
			} ]
			listdata.addData(noData);
		}
		else
			processData(data);
		listview.refreshItems();// 刷新item数据
	});
}
function getNextPageData() {
	page++;
	var params = {
		"lastClick": lastDate,
		"currentClick": parseInt(new Date().getTime()/1000).toString(),
		page : page,
		communityId : communityUtil.communityVisitId(),
		interests : paramIn.interestKey,
		sex : paramIn.sexKey,
		profession:paramIn.industryKey,
		delicacys:paramIn.delicacyKey,
		hometown:paramIn.hometownKey,
		entertainment:paramIn.entertainmentKey
	}
	qqHttp.post(httpUrl, params, function(data) {
		listview.rebound();
		processData(data);
		listview.refreshItems();	
	});
}

// 下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		isFriend=true;
		isOther=true;
		isLast=true;
		isLastLast=true;
		binddata();
	}
});

// 上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 1) {
		getNextPageData();
	}
});
// 点击一个用户
listview.on("touch","",300, function(data) {
	var user = listdata.getOne(data);
	var userId = user.id;
	if (userId) {
		openHelper.openPersonal(userId);
	}
});
